/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.spring.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.mai.antexpress.dao.DepartmentDAO;
import team.mai.antexpress.dao.UserDAO;
import team.mai.antexpress.entities.DepartmentEntity;
import team.mai.antexpress.services.DepartmentService;
import team.mai.antexpress.services.UserService;
import team.mai.antexpress.spring.beans.LoginBean;

import static team.mai.antexpress.services.DepartmentService.*;

import java.util.List;

@Controller
public class DepartmentController {
	@Autowired
	DepartmentService departmentService;
	@Autowired
	LoginBean login;
	@Autowired
	UserService userService;
	@Autowired
	DepartmentDAO departmentDAO;
	@Autowired
	UserDAO userDAO;

	@RequestMapping(value = "/admin/manage_departments", method = RequestMethod.GET)
	public String doGet(ModelMap map) {
		List<DepartmentEntity> allDepartments = departmentService.getAllDepartments(login.getUsername());
		map.addAttribute("depts", allDepartments);
		map.addAttribute("permissions", userDAO.getPermissionByUser(login.getUsername()));
		return "admin/manage_departments";
	}

	@RequestMapping(value = "/admin/new_department", method = RequestMethod.GET)
	public String newDeptPage() {
		return "admin/new_deparment";
	}

	@RequestMapping(value = "/admin/new_department", method = RequestMethod.POST)
	@ResponseBody
	public String insertDept(DepartmentEntity dept) {
		int id = departmentService.insertDepartment(login.getUsername(), dept);
		if (id > 0) {
			return "{\"status\":\"success\", \"id\":" + id + "}";
		} else {
			StringBuilder stringBuilder = new StringBuilder("{\"status\":\"error\", \"error\":");
			stringBuilder.append("\"");
			if (id == DepartmentService.ERROR)
				stringBuilder.append("Internal Error");
			else if (id == DepartmentService.INSUFFICIENT_PRIVILEGE)
				stringBuilder.append("Insufficient Privilege");
			stringBuilder.append("\"}");
			return stringBuilder.toString();
		}
	}

	@RequestMapping(value = "/admin/ajax/check_department_name", method = RequestMethod.POST)
	@ResponseBody
	public String checkDepartmentName(String except, String deptName) {
		if (departmentDAO.ifExistsDepartment(except, deptName)) {
			return "{\"exists\":true}";
		} else {
			return "{\"exists\":false}";
		}
	}

	@RequestMapping(value = "/admin/delete_department", method = RequestMethod.POST)
	@ResponseBody
	public String deleteDepartment(int id) {
		int errorCode = departmentService.deleteDepartment(login.getUsername(), id);
		String status;
		switch (errorCode) {
			case SUCCESS:
				status = "ok";
				break;
			case ERROR:
				status = "error";
				break;
			case INSUFFICIENT_PRIVILEGE:
				status = "error";
				break;
			case USER_IN_DEPARTMENT:
				status = "error";
				break;
			default:
				status = "error";
		}
		return "{\"status\":\"" + status + "\",\"errorCode\":" + errorCode + "}";
	}

	@RequestMapping(value = "/admin/delete_users_in_dept", method = RequestMethod.POST)
	@ResponseBody
	public String deleteUsersInDept(int id) {
		int i = departmentService.deleteUsersInDepartment(login.getUsername(), id);
		String status;
		String errorMessage = "";
		switch (i) {
			case SUCCESS:
				status = "ok";
				break;
			case ERROR:
				status = "error";
				errorMessage = "Internal Error";
				break;
			case INSUFFICIENT_PRIVILEGE:
				status = "error";
				errorMessage = "Insufficient Privilege";
				break;
			default:
				status = "error";
				;
				errorMessage = "Internal Error";
		}
		return "{\"status\":\"" + status + "\",\"errorMessage\":\"" + errorMessage + "\"}";
	}

	@RequestMapping(value = "/admin/modify_department", method = RequestMethod.GET)
	public String getModifyDepartment(@RequestParam("deptId") int deptId, ModelMap map) {
		DepartmentEntity department = departmentService.getDepartment(login.getUsername(), deptId);
		map.addAttribute("dept", department);
		if (department == null) {
			map.addAttribute("errorMessage",
					"Failed to fetch department #" + deptId + ".");
		}
		return "admin/modify_department";
	}

	@RequestMapping(value = "/admin/modify_department", method = RequestMethod.POST)
	@ResponseBody
	public String postModifyDepartment(DepartmentEntity dept) {
		switch (departmentService.updateDepartment(login.getUsername(), dept)) {
			case SUCCESS:
				return "{\"status\":\"success\"}";
			case ERROR:
				return "{\"status\":\"fail\",\"errorMessage\":\"Internal Error\"}";
			case INSUFFICIENT_PRIVILEGE:
				return "{\"status\":\"fail\",\"errorMessage\":\"Insufficient Privilege\"}";
			default:
				return "{\"status\":\"fail\",\"errorMessage\":\"Internal Error\"}";
		}
	}


}
